package com.engine.cetc.duss.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaResourceWeb extends BaseBean {
    /**
     * @Description: 修改人员部门或者岗位
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2024/04/17 09:06
     */
    @POST
    @Path("/doUpdateResouece")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String doUpdateResouece(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            writeLog(">>>>>doUpdateResouece>>>paramsJson=" + paramsJson);
            JSONArray datalist = paramsJson.getJSONArray("datalist");
            writeLog(">>>>>doUpdateResouece>>>datalist=" + datalist);
            JSONObject resultJson = updateDepartment(datalist);
            writeLog(">>>>>doUpdateResouece>>>resultJson=" + resultJson);
            apidatas.put("data", resultJson);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "请求接口异常");
        }
        return JSONObject.toJSONString(apidatas);
    }


    private static JSONObject updateDepartment(JSONArray datalist) throws Exception {
        StringBuilder message = new StringBuilder();
        JSONObject resultJosn = new JSONObject();
        RecordSet rs = new RecordSet();
        int totalcounts = datalist.size();
        int succcounts = 0;
        for (int i = 0; i < datalist.size(); i++) {
            String loginid = Util.null2String(datalist.getJSONObject(i).getString("loginid"));
            String departmentcode = Util.null2String(datalist.getJSONObject(i).getString("departmentcode"));
            rs.executeQuery("select deptid from hrmdepartmentdefined where rsxtbmbh=?", departmentcode);
            if (rs.next()) {
                String deptid = Util.null2String(rs.getString(1));
                boolean flag = rs.executeUpdate("update hrmresource set departmentid=? where loginid=?", deptid, loginid);
                if (flag) {
                    ResourceComInfo resourceComInfo = new ResourceComInfo();
                    resourceComInfo.removeResourceCache();
                    succcounts++;
                } else {
                    message.append(loginid).append(":人员,更新信息失败,所属的部门编码：").append(departmentcode);
                }
            } else {
                message.append(loginid).append(":部门编码不存在");
            }
        }
        resultJosn.put("totalcounts", totalcounts);
        resultJosn.put("succcounts", succcounts);
        resultJosn.put("message", message);
        return resultJosn;
    }

}
